package com.lld.controller;

import com.lld.entity.User;
import com.lld.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: memo
 * @description: 用户控制器
 * @author: lld
 * @create: 2020-10-09 20:54
 **/
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public Map<String, Object> login(String username, String password, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        try {
            User user = userService.login(username, password);
            session.setAttribute("user", user);
            map.put("state", 1);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", 0);
        }
        return map;
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Map<String, Object> register(User user) {
        Map<String, Object> map = new HashMap<>();
        try {
            userService.register(user);
            map.put("state", 1);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", 0);
        }
        return map;
    }

}
